package com.cusc.module_apply.mvvm.ui;

import android.content.Intent;
import android.view.Menu;

import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.cusc.module_apply.BR;
import com.cusc.module_apply.R;
import com.cusc.module_apply.adapter.ApplyMainVPAdapter;
import com.cusc.module_apply.databinding.ActApplyMainBinding;
import com.cusc.module_apply.factory.ApplyTabItemViewFactory;
import com.cusc.module_apply.factory.ApplyViewModuleFactory;
import com.cusc.module_apply.mvvm.ui.activity.ApplyMainSearchActivity;
import com.cusc.module_apply.mvvm.ui.fragment.ApplyMainFragment;
import com.cusc.module_apply.mvvm.vm.ApplyActMainViewModel;
import com.google.android.material.tabs.TabLayout;
import com.tcgroup.phone.base_core.event.event_router.RouterPath;
import com.tcgroup.phone.base_core.framework.mvvm.activity.ContainerMvvmActivity;
import com.tcgroup.phone.base_core.provider.IApplyMainProvider;

import java.util.ArrayList;

/**
 * 作者: wangfei
 * 创建时期: 2020/3/27.
 * 包名: com.cusc.module_apply.mvvm.view
 * 类名: OSAndroidMvvm
 */
@Route(path = RouterPath.Apply_Router.Apply_ACT_Main)
public class ApplyMainActivity extends ContainerMvvmActivity<ActApplyMainBinding, ApplyActMainViewModel> {

    @Autowired(name = RouterPath.Apply_Router.Apply_Frag_Main)
    IApplyMainProvider mIApplyMainProvider;
    public ApplyTabItemViewFactory factory;
    public ArrayList<ApplyMainFragment> applyMainFragments = new ArrayList<>();

    @Override
    public int onBindVariableId() {
        return BR.model;
    }

    @Override
    public Class<ApplyActMainViewModel> onBindViewModel() {
        return ApplyActMainViewModel.class;
    }

    @Override
    public ViewModelProvider.Factory onBindViewModelFactory() {
        return ApplyViewModuleFactory.getInstance(getApplication());
    }

    @Override
    public void initListener() {
        super.initListener();

    }


    @Override
    public void initViewObservable() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.layout_search_add,menu);
        return true;
    }

    @Override
    public void initContentView() {
        super.initContentView();
        for (int i = 0;i<4;i++){
            ApplyMainFragment applyMainFragment = ApplyMainFragment.newInstance();
            applyMainFragments.add(applyMainFragment);
        }
        ApplyMainVPAdapter applyMainVPAdapter = new ApplyMainVPAdapter(getSupportFragmentManager(),applyMainFragments);
        mBinding.viewpager.setAdapter(applyMainVPAdapter);
        mBinding.viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mBinding.tabLayout));

        mBinding.tabLayout.setViewPager(mBinding.viewpager);
        factory = new ApplyTabItemViewFactory(this);
        mBinding.tabLayout.createItemFactory(factory);
        mBinding.tabLayout.createItemModelList(mViewModel.tabLayout);

        mToolbar.inflateMenu(R.menu.layout_search_add);
        mToolbar.setOnMenuItemClickListener(item -> {
            if (item.getItemId() == R.id.menu_add){

                return true;
            }
            if (item.getItemId() == R.id.menu_search){
                Intent intent = new Intent(this, ApplyMainSearchActivity.class);
                startActivity(intent);
                finish();
                return true;
            }
            return true;
        });
    }


    @Override
    public void onRetryBtnClick() {

    }

    @Override
    public void onBackPressedClick() {

    }

    @Override
    public int onBindLayout() {
        return R.layout.act_apply_main;
    }

    @Override
    public Object bindLoadSir() {
        return null;
    }


}
